package fr.fbhelp.parsing
{
	import fr.fbhelp.files.CopyFilesQueue;
	import fr.fbhelp.files.FileManager;

	public class HelpPackageStore
	{
		public var packages:Array = [];
		
		public function HelpPackageStore()
		{
		}
		
		public function addPackageFromXML(pkgXml:XML):void
		{
			var hp:HelpPackage = new HelpPackage();
			hp.parseFromXML(pkgXml);
			packages.push(hp);
		}
		
		public function createTocFromContents():void
		{
			for each (var p:HelpPackage in packages)
			{
				p.parseTocFromContents();
			}
		}

		/**
		 * Saves the generated Help Toc as file.
		 * <?xml version="1.0" encoding="UTF-8"?>
		 * <?NLS TYPE="org.eclipse.help.toc"?>
		 * <!-- ============================================================================= -->
		 * <!-- Define the top level topics                                                   -->
		 * <!-- ============================================================================= -->
		 * 
		 * <toc label="Workbench User Guide">
		 *  <topic label="Eclipse platform overview" href="gettingStarted/intro/overview.htm"/>
		 * 	<topic label="Getting started">
		 * 		<link toc="topics_GettingStarted.xml" />
		 * 	</topic>
		 * </toc>
		 */
		public function createTopLevelToc(baseEclipseHelpFolderNativePath:String, nativeSeparator: String, fileUtil:FileManager):void
		{
			var copyQueue:CopyFilesQueue = new CopyFilesQueue();
			
			var xmlString:String = getTocHeader();
			for each (var p:HelpPackage in packages)
			{
				if (p.toc.generatedTocSection != null)
				{
					xmlString += p.toc.generatedTocSection + '\n';
					
					copyQueue.addItem(p.toc.basePath, baseEclipseHelpFolderNativePath + nativeSeparator + "html" + nativeSeparator + p.toc.folderName);
				}
			}
			xmlString += '</toc>';
			
			Output.append("\nSaving the toc.xml file backup copy...");
			fileUtil.backupFile(baseEclipseHelpFolderNativePath + nativeSeparator + "toc.xml");
			Output.append("\nSaving the toc.xml file...");
			fileUtil.saveFile(xmlString, baseEclipseHelpFolderNativePath + nativeSeparator + "toc.xml");
			
			copyQueue.initCopying();
		}
		
		[Deprecated]
		public function createTocWithExternalLinks(baseEclipseHelpFolderNativePath:String, nativeSeparator:String, fileUtil:FileManager):void
		{
			var xmlString:String = getTocHeader();
			for each (var p:HelpPackage in packages)
			{
				if (p.toc.generatedTocSection != null)
				{
					xmlString += '<topic label="' + p.label + '">\n';
					xmlString += '	<link toc="' + p.toc.fileName + '"/>\n';
					xmlString += '</topic>\n';
				}
			}
			xmlString += '</toc>';
			fileUtil.saveFile(xmlString, baseEclipseHelpFolderNativePath + nativeSeparator + "toc.xml");
		}
		
		private function getTocHeader():String
		{
			return '' +
				'<?xml version="1.0" encoding="UTF-8"?> \n' +
				'<?NLS TYPE="org.eclipse.help.toc"?> \n' +
				'<!-- ============================================================================= --> \n' +
				'<!-- Define the top level topics                                                   --> \n' +
				'<!-- ============================================================================= --> \n' +
				'<toc label="Flash Builder Help (Friendly Again)"> \n';
		}
		
		public function removeInexistentPackages():void
		{
			trace ("Help packages total: " + packages.length);
			
			for (var i:Number = 0; i < packages.length; i++)
			{
				var p:HelpPackage = packages[i];
				if (p.toc == null)
				{
					Output.append("\tIgnoring package (probably it has no local content downloaded or TOC source): " + p.label);
					//Log.append("\tRemove inexistent package: " + pkg.label);
					//var idx:Number = packages.indexOf(p);
					packages.splice(i,1);
					i = i-1;
				}				
			}
			trace ("Help packages exist: " + packages.length);
		}
	}
}